﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Hellquest
{
    public class Particle
    {
        public Rigidbody rigidbody;

        public float Rotation;

        public Color Color;
        public Vector2 Scale;

        public float MaxLifetime;

        public float Lifetime { get; private set; }
        public Animation animation;
        private float rotationSpeed;

        public Particle(Vector2 _position, Vector2 _velocity, Vector2 _scale, float _rotation, float _rotationSpeed, Color _color, float _maxLifetime, Animation _anim)
        {
            animation = _anim;
            rigidbody = new Rigidbody(10);
            rigidbody.Position = _position;
            rigidbody.ApplyImpulse(_velocity);
            PhysicsEngine.Instance.AddRigidbody(rigidbody);

            Scale = _scale;
            Color = _color;
            MaxLifetime = _maxLifetime;
            Rotation = _rotation;
            rotationSpeed = _rotationSpeed;
        }

        public void SetValues(Vector2 _position, Vector2 _velocity, Vector2 _scale, float _rotation, float _rotationSpeed, Color _color, float _maxLifetime, Animation _anim)
        {
            Lifetime = 0;
            animation = _anim;
            rigidbody.Position = _position;
            rigidbody.ApplyImpulse(_velocity);

            Scale = _scale;
            Color = _color;
            MaxLifetime = _maxLifetime;
            Rotation = _rotation;
            rotationSpeed = _rotationSpeed;
        }

        public void SetActive(bool _active)
        {
            if (_active)
                rigidbody.IsActive = true;
            rigidbody.IsActive = _active;
        }

        public void Update(GameTime _gameTime, Vector2 _startScale, Color[] _animationColors, Vector2 _endScale, float _startRotation, float _endRotation)
        {
            if (rigidbody.IsActive)
            {
                Lifetime += (float)_gameTime.ElapsedGameTime.TotalSeconds;

                if (Lifetime / MaxLifetime <= 0.25f)
                {
                    Color = Color.Lerp(_animationColors[0], _animationColors[1], (Lifetime / MaxLifetime) / 0.25f);
                }
                else if (Lifetime / MaxLifetime <= 0.5f)
                {
                    Color = Color.Lerp(_animationColors[1], _animationColors[2], (Lifetime / MaxLifetime - 0.25f) / 0.25f);
                }
                else if (Lifetime / MaxLifetime <= 0.75f)
                {
                    Color = Color.Lerp(_animationColors[2], _animationColors[3], (Lifetime / MaxLifetime - 0.5f) / 0.25f);
                }
                else
                {
                    Color = Color.Lerp(_animationColors[3], _animationColors[4], (Lifetime / MaxLifetime - 0.75f) / 0.25f);
                }

                Scale = Vector2.Lerp(_startScale, _endScale, Lifetime / MaxLifetime);
                Rotation += rotationSpeed * (float)_gameTime.ElapsedGameTime.TotalSeconds;
                animation.Update(_gameTime);
                rigidbody.ApplyForce(new Vector2(0, -10));
            }
        }        

        

    }
}
